import os

file_name = input('enter an old file name:')
while True:
    if os.path.isfile(file_name):
        old_file = open(file_name, mode='rb')
        # 根据原有文件名，生成新文件的名字
        # 1.
        # names = file_name.rpartition('.')  # 不要用partition
        # new_file_name = names[0] + '.bak.' + names[2]
        # 2.
        names = os.path.splitext(file_name)
        new_file_name = names[0] + '.bak' + names[1]
        new_file = open(new_file_name, 'wb')

        # 1.一次全部读完:文件太大时会极其消耗内存
        # new_file.write(old_file.read())
        # 2.一次读长度为n的内容
        while True:
            stream = old_file.read(16)
            if not stream:
                break
            new_file.write(stream)
        old_file.close()
        new_file.close()
        break
    else:
        print('file not exist')
        file_name = input('enter an old file name:')

new_file = open(file_name, mode='rb')
print(new_file.read())
